﻿using System;
using System.IO;
using System.Web.Hosting;

namespace Service.Web
{
  public class Global : System.Web.HttpApplication
  {

    protected void Application_Start(object sender, EventArgs e)
    {

    }

    protected void Session_Start(object sender, EventArgs e)
    {
      //-- products ------------------------
      var li_product = new ProductList();
      string path = HostingEnvironment.ApplicationPhysicalPath + @"App_Data\Product.csv";
      using(StreamReader sr = new StreamReader(path)) {
        sr.ReadLine(); // no first row
        while(!sr.EndOfStream) {
          string[] ass = sr.ReadLine().Split(';');

          decimal standardCost, listPrice, weight;
          int size;
          decimal.TryParse(ass[4], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out standardCost);
          decimal.TryParse(ass[5], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out listPrice);
          decimal.TryParse(ass[7], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out weight);
          int.TryParse(ass[6], System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out size);
          Product product = new Product() {
            ProductID = Guid.NewGuid(),
            Name = ass[1],
            ProductNumber = ass[2],
            Color = ass[3],
            StandardCost = standardCost, //ass[4],
            ListPrice = listPrice, //ass[5],
            Size = size, // ass[6],
            Weight = weight, //ass[7],
            SellStartDate = DateTime.Parse(ass[8]),

          };
          li_product.Add(product);
        }
      }
      Session.Add("products_odata", li_product);
    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {

    }

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {

    }

    protected void Application_Error(object sender, EventArgs e)
    {

    }

    protected void Session_End(object sender, EventArgs e)
    {

    }

    protected void Application_End(object sender, EventArgs e)
    {

    }
  }
}